<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 会员类
 */
class Member extends CI_Controller
{

    /**
     * 判断用户是否登录
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
        parent::__construct();

        $this->load->model('model_member', 'member');

        $this->member->init();
        if ($this->member->is_login() && $this->input->get('m') != 'logout')
        {
            $this->member->goto_album();
        }
    }

    /**
     * 用QQ帐号登录
     *
     * @access public
     * @return void
     */
    public function index()
    {
        $this->load->view('member_login');
    }

    /**
     * 退出登录
     *
     * @access public
     * @return void
     */
    public function logout()
    {
        // 会员退出
        $this->member->logout();
        // 跳转到会员登录页
        $this->member->goto_login();
    }

    /**
     * QQ互联授权回调函数
     *
     * @access public
     * @return void
     */
    public function callback()
    {
        // 使用 Authorization Code 获取 Access Token
        $response = $this->member->get_qq_access_token($this->input->get('code'));
        if ($response['error_code'])
        {
            exit($response['error_code'] . ':' . $response['msg']);
        }

        // 使用 Access Token 来获取用户的 OpenID
        $token    = $response['access_token'];
        $response = $this->member->get_qq_openid($token);
        if ($response['error_code'])
        {
            exit($response['error_code'] . ':' . $response['msg']);
        }

        // 使用 Access Token 以及 OpenID 访问用户数据
        $openid   = $response['openid'];
        $response = $this->member->get_qq_user_info($token, $openid);
        if ($response['ret'])
        {
            exit($response['ret'] . ':' . $response['msg']);
        }

        // 会员登录
        $this->member->login($openid, $response['nickname']);
        // 跳转到我的相册
        $this->member->goto_album();
    }
}